package com.sinog.biz.util;

import com.sinog.biz.mapper.havexml.PrisonerMainMapper;
import com.sinog.core.utilbean.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @desc 业务代码工具类封装
 * @author lzq
 * @date 2022/11/8 15:50
 */
@Slf4j
public class BusinessUtils {

    /**
     * 生成犯罪编号
     * @param prisonerMainMapper prisonerMainMapper
     * @param stringRedisTemplate stringRedisTemplate
     * @param departId departId
     * @param levelParam levelParam
     * @return String
     */
    public static String makePriNum(PrisonerMainMapper prisonerMainMapper,StringRedisTemplate stringRedisTemplate,String departId,String levelParam) {
        // 1、定义开始时间
        long start = System.currentTimeMillis();
        // 2、定义生成的犯罪编号
        String priNum;
        // 3、声明犯罪编号前7位变量
        String depart;
        // 4、取出接口用户部门ID
        String jsDepartid = departId;
        // 5、取出接口用户level
        // 6、生成犯罪编号前7位depart
        if(Constants.NumberStr.NUM_STR_ZERO_ONE.equals(levelParam)) {
            jsDepartid += Constants.NumberStr.NUM_STR_ZERO;
        }
        if(Constants.Number.NUM_FIVE == jsDepartid.length()) {
            depart = jsDepartid + levelParam;
        } else {
            depart = jsDepartid.substring(0,5) + levelParam;
        }
        // 7、正确生成前7位，拼接4位日期，拼接5位序号
        if(Constants.Number.NUM_SEVEN == depart.length()) {
            StringBuilder prinum = new StringBuilder(16);
            SimpleDateFormat simformat = new SimpleDateFormat("yyyyMMdd");
            String dateformat = simformat.format(new Date());
            // 获取当前年份和月份 19代表2019年
            String dateStr = dateformat.substring(2,4);
            long prinum1 = stringRedisTemplate.opsForHash().increment("prinum",depart + dateStr,1L);
            String seqCodeEnd = Long.toString(prinum1);
            // 更改服刑人员编号生成规则
            while(Constants.Number.NUM_SEVEN > seqCodeEnd.length()) {
                seqCodeEnd = new StringBuilder(16).append(Constants.NumberStr.NUM_STR_ZERO).append(seqCodeEnd)
                                                  .toString();
            }
            prinum.append(depart).append(dateStr).append(seqCodeEnd);
            priNum = prinum.toString();
            if(Constants.Number.NUM_TWO << 3 != priNum.length()) {
                log.error("生成的罪犯编号{}不正确",priNum);
                return Constants.StrFinalValue.ERROR;
            }
            //检查生成的犯罪编号数据库中是否已经存在，存在继续生成
            String s = prisonerMainMapper.existPriNum(priNum);
            while(!StringUtils.isEmpty(s)) {
                StringBuilder numprinum = new StringBuilder(16);
                prinum1 = stringRedisTemplate.opsForHash().increment("prinum",depart + dateStr,1);
                seqCodeEnd = Long.toString(prinum1);
                while(Constants.Number.NUM_SEVEN > seqCodeEnd.length()) {
                    seqCodeEnd = new StringBuilder(16).append(Constants.NumberStr.NUM_STR_ZERO).append(seqCodeEnd)
                                                      .toString();
                }
                numprinum.append(depart).append(dateStr).append(seqCodeEnd);
                priNum = numprinum.toString();
                s = prisonerMainMapper.existPriNum(priNum);
            }
        } else {
            log.error("生成服刑人员编号{}前7位格式不正确。",depart);
            return Constants.StrFinalValue.ERROR;
        }
        log.info("【生成犯罪编号耗时】:{} ms",System.currentTimeMillis() - start);
        return priNum;
    }

    /**
     * 跨省收取整理sql数据
     * @param priNum 罪犯编号
     * @param localCode 本省编码
     * @param huji 目的省份编码
     * @param middleJdbcTemplate 司法部中间库sql执行器
     */
    public static void handleMiddleProvinceData(Object priNum,Object localCode,Object huji,JdbcTemplate middleJdbcTemplate) {
        String prisonerMainDeleteSql = "delete from prisoner_main where pri_num=\'" + priNum + "\'";
        String prisaveTempDeleteSql = "delete from prisave_temp where pri_num=\'" + priNum + "\'";
        String prisonerFxDeleteSql = "delete from prisoner_fx where pri_num=\'" + priNum + "\'";
        String prisonerHcDeleteSql = "delete from prisoner_hc where pri_num=\'" + priNum + "\'";
        String prisonerSrDeleteSql = "delete from prisoner_sr where pri_num=\'" + priNum + "\'";
        String prisonerZnDeleteSql = "delete from prisoner_zn where pri_num=\'" + priNum + "\'";
        String prisonerZzDeleteSql = "delete from prisoner_zz where pri_num=\'" + priNum + "\'";
        String uploadAppendixFileDeleteSql = "delete from upload_appendix_file where pri_num=\'" + priNum + "\'";
        String xjReceiptDeleteSql = "delete from xj_receipt where pri_num='" + priNum + "'";
        String xjReceiptDataDeleteSql = "delete from xj_receipt_data where session_id in (select xjid from xj_receipt where pri_num ='" + priNum + "')";
        String middleSql1 = JdbcUtils.createMiddleSql(priNum,prisonerMainDeleteSql,Constants.Number.NUM_ZERO,localCode,huji);
        middleJdbcTemplate.update(middleSql1);
        String middleSql2 = JdbcUtils.createMiddleSql(priNum,prisaveTempDeleteSql,Constants.Number.NUM_ZERO,localCode,huji);
        middleJdbcTemplate.update(middleSql2);
        String middleSql3 = JdbcUtils.createMiddleSql(priNum,prisonerFxDeleteSql,Constants.Number.NUM_ZERO,localCode,huji);
        middleJdbcTemplate.update(middleSql3);
        String middleSql4 = JdbcUtils.createMiddleSql(priNum,prisonerHcDeleteSql,Constants.Number.NUM_ZERO,localCode,huji);
        middleJdbcTemplate.update(middleSql4);
        String middleSql5 = JdbcUtils.createMiddleSql(priNum,prisonerSrDeleteSql,Constants.Number.NUM_ZERO,localCode,huji);
        middleJdbcTemplate.update(middleSql5);
        String middleSql6 = JdbcUtils.createMiddleSql(priNum,prisonerZnDeleteSql,Constants.Number.NUM_ZERO,localCode,huji);
        middleJdbcTemplate.update(middleSql6);
        String middleSql7 = JdbcUtils.createMiddleSql(priNum,prisonerZzDeleteSql,Constants.Number.NUM_ZERO,localCode,huji);
        middleJdbcTemplate.update(middleSql7);
        String middleSql8 = JdbcUtils.createMiddleSql(priNum,uploadAppendixFileDeleteSql,Constants.Number.NUM_ZERO,localCode,huji);
        middleJdbcTemplate.update(middleSql8);
        String middleSql9 = JdbcUtils.createMiddleSql(priNum,xjReceiptDeleteSql,Constants.Number.NUM_ZERO,localCode,huji);
        middleJdbcTemplate.update(middleSql9);
        String middleSql10 = JdbcUtils.createMiddleSql(priNum,xjReceiptDataDeleteSql,Constants.Number.NUM_ZERO,localCode,huji);
        middleJdbcTemplate.update(middleSql10);
        List<String> prisonerMainInsertSql = JdbcUtils.getInsertSql("prisoner_main","pri_num",priNum,null,null,null,null);
        List<String> prisaveTempinsertSql = JdbcUtils.getInsertSql("prisave_temp","pri_num",priNum,null,null,null,null);
        List<String> prisonerFxinsertSql = JdbcUtils.getInsertSql("prisoner_fx","pri_num",priNum,null,null,null,null);
        List<String> prisonerHcinsertSql = JdbcUtils.getInsertSql("prisoner_hc","pri_num",priNum,null,null,null,null);
        List<String> prisonerSrinsertSql = JdbcUtils.getInsertSql("prisoner_sr","pri_num",priNum,null,null,null,null);
        List<String> prisonerZninsertSql = JdbcUtils.getInsertSql("prisoner_zn","pri_num",priNum,null,null,null,null);
        List<String> prisonerZzinsertSql = JdbcUtils.getInsertSql("prisoner_zz","pri_num",priNum,null,null,null,null);
        List<String> xjReceiptInsertSql = JdbcUtils.getInsertSql("xj_receipt","pri_num",priNum,null,null,null,null);
        List<String> xjReceiptDataInsertSql = JdbcUtils.getInsertSql1("xj_receipt_data","session_id",priNum,null,null,null,null);
        List<String> uploadAppendixFileInsertSql = JdbcUtils.getInsertSql("upload_appendix_file","pri_num",priNum,null,null,null,null);
        for(String sql : prisonerMainInsertSql) {
            String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,huji);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : prisaveTempinsertSql) {
            String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,huji);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : prisonerFxinsertSql) {
            String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,huji);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : prisonerHcinsertSql) {
            String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,huji);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : prisonerSrinsertSql) {
            String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,huji);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : prisonerZninsertSql) {
            String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,huji);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : prisonerZzinsertSql) {
            String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,huji);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : uploadAppendixFileInsertSql) {
            String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,huji);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : xjReceiptDataInsertSql) {
            String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,huji);
            middleJdbcTemplate.update(isql);
        }
        for(String sql : xjReceiptInsertSql) {
            String isql = JdbcUtils.createMiddleSql(priNum,sql,Constants.Number.NUM_ZERO,localCode,huji);
            middleJdbcTemplate.update(isql);
        }
    }
}